package com.mmg.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import com.mmg.spzx.manager.service.ProductService;
import com.mmg.spzx.model.dto.product.ProductDto;
import com.mmg.spzx.model.entity.product.Product;
import com.mmg.spzx.model.vo.common.Result;
import com.mmg.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "商品管理")
@RestController
@RequestMapping(value = "/admin/product/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Operation(summary = "修改商品上架状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,
                               @PathVariable Integer status) {
        productService.updateStatus(id, status);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改商品审核状态")
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result updateAuditStatus(@PathVariable Long id,
                                    @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据id删除商品")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        productService.delete(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改商品")
    @PutMapping("/update")
    public Result update(@RequestBody Product product) {
        productService.update(product);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据id查询商品")
    @GetMapping("/findById/{id}")
    public Result<Product> findById(@PathVariable Long id) {
        Product product = productService.findById(id);
        return Result.build(product, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "新增商品")
    @PostMapping("/save")
    public Result save(@RequestBody Product product) {
        productService.save(product);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<PageInfo<Product>> findByPage(@PathVariable Integer page,
                                                @PathVariable Integer limit,
                                                ProductDto productDto) {
        PageInfo<Product> pageInfo = productService.findByPage(page, limit, productDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }
}
